﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.UpdateContinuousMfg.CApUCMUpdateContinuousMfgTCLogic
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using System;
using System.Collections.ObjectModel;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.UpdateContinuousMfg;

public class CApUCMUpdateContinuousMfgTCLogic
{
  protected const string _PRESERVED_ATTRIBUTE = "Preserved";
  protected const int _EMPHASIZED_SEAMS_WIDTH = 3;
  protected const int _MAX_DATASET_NAME = 32 /*0x20*/;
  protected TxSeamMfgFeature _selectedSeam;

  public TxSeamMfgFeature SelectedSeam
  {
    get => this._selectedSeam;
    private set => this._selectedSeam = value;
  }

  public CApUCMUpdateContinuousMfgTCLogic() => this.InitSelectedMfg();

  protected void InitSelectedMfg()
  {
    this.SelectedSeam = ((Collection<ITxObject>) TxApplication.ActiveSelection.GetAllItems())[0] as TxSeamMfgFeature;
  }

  public void SelectTheSeamMfg()
  {
    TxApplication.ActiveSelection.Clear();
    TxObjectList txObjectList = new TxObjectList();
    ((Collection<ITxObject>) txObjectList).Add((ITxObject) this.SelectedSeam);
    TxApplication.ActiveSelection.AddItems(txObjectList);
    TxApplication.RefreshDisplay();
  }

  private void EmphasizeSeamIfWeAreInEmphasizeMode()
  {
    if (TxApplication.CommandsManager.IsCommandChecked("CApECMEmphasizeContinuousMfgCmd") && TxSeamMfgFeature.op_Inequality(this._selectedSeam, (ITxObject) null))
      this._selectedSeam.SetLineWidth(3);
    TxApplication.RefreshDisplay();
  }

  protected string CreateDataSetName()
  {
    string empty = string.Empty;
    string str = DateTime.Now.ToString();
    string dataSetName = $"{((TxMfgFeature) this.SelectedSeam).Name}_{str}";
    if (dataSetName.Length > 32 /*0x20*/)
      dataSetName = $"{((TxMfgFeature) this.SelectedSeam).Name.Substring(0, 32 /*0x20*/ - str.Length - 2)}_{str}";
    return dataSetName;
  }

  public bool IsObjectSimilarToOperationRoot(ITxObject sourceObject)
  {
    bool operationRoot = false;
    if (sourceObject != null && sourceObject == TxApplication.ActiveDocument.OperationRoot)
      operationRoot = true;
    return operationRoot;
  }

  public bool ShouldEnableOKButton(ITxObject curve)
  {
    bool flag = false;
    if (curve != null)
    {
      if (!this.IsObjectSimilarToOperationRoot(curve))
        flag = true;
      if (curve == this.SelectedSeam)
        flag = false;
    }
    return flag;
  }

  public void ReloadComponent()
  {
    TxApplication.ActiveUndoManager.ClearAllTransactions();
    new TxSeamMfgFeatureEx(this.SelectedSeam).Reload();
    TxApplication.ActiveUndoManager.ClearAllTransactions();
  }

  public void UpdateChangesToRevision()
  {
    TxApplication.ActiveDocument.UndoManager.ClearAllTransactions();
    new TxSeamMfgFeatureEx(this.SelectedSeam)?.CopyScopeToRevision();
    TxApplication.ActiveDocument.UndoManager.ClearAllTransactions();
  }

  public bool ChangeCurve(ITxObject newCurve)
  {
    bool flag = false;
    if (newCurve != this.SelectedSeam)
    {
      if (newCurve == TxApplication.ActiveDocument.OperationRoot)
        this.ReloadComponent();
      else if (newCurve != null)
      {
        TxApplication.ActiveUndoManager.StartTransaction();
        TxDisplayableObjectVisibility visibility = (newCurve as ITxDisplayableObject).Visibility;
        (newCurve as ITxDisplayableObject).Display();
        newCurve.RemoveAttribute("Preserved");
        flag = TxApplication.ActiveDocument.MfgRoot.UpdateSeamMfgFeature(this.SelectedSeam, newCurve);
        if (visibility == 2)
          (newCurve as ITxDisplayableObject).Blank();
        this.EmphasizeSeamIfWeAreInEmphasizeMode();
        TxApplication.ActiveUndoManager.EndTransaction();
      }
    }
    return flag;
  }
}
